package groups.geographics.parser;

import groups.common.DBDumper;
import groups.geographics.Country;
import groups.geographics.dao.ICountryDao;
import groups.geographics.dao.JDBCCountryDao;

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ListIterator;
import java.util.Vector;

import org.htmlparser.*;
import org.htmlparser.filters.LinkStringFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;

public class CountryParser {
	private static URL url;
	private static Parser parser;

	public static Vector<Country> parse() throws ParserException, IOException{
		Vector<Country> countries = new Vector<Country>();
		url = new URL("http://www.worldatlas.com/cntycont.htm");
		parser = new Parser(url.openConnection());
		NodeList nodeList = parser.parse(new TagNameFilter("LI"));
		
		//Insercao dos paises da Africa
		NodeList countryList = nodeList.extractAllNodesThatMatch(
				new LinkStringFilter("http://www.worldatlas.com/webimage/countrys/africa/"), true);
		SimpleNodeIterator iterator = countryList.elements();
		while (iterator.hasMoreNodes()){
			Country country = new Country();
			country.setName(((LinkTag)iterator.nextNode()).getLinkText());
			country.setContinent("Africa");
			countries.add(country);
		}
		
		//Insercao dos paises da Asia
		countryList = nodeList.extractAllNodesThatMatch(
				new LinkStringFilter("http://www.worldatlas.com/webimage/countrys/asia/"), true);
		iterator = countryList.elements();
		while (iterator.hasMoreNodes()){
			Country country = new Country();
			country.setName(((LinkTag)iterator.nextNode()).getLinkText());
			country.setContinent("Asia");
			countries.add(country);
		}
		
		//Insercao dos paises da Europa
		countryList = nodeList.extractAllNodesThatMatch(
				new LinkStringFilter("http://www.worldatlas.com/webimage/countrys/europe/"), true);
		iterator = countryList.elements();
		while (iterator.hasMoreNodes()){
			Country country = new Country();
			country.setName(((LinkTag)iterator.nextNode()).getLinkText());
			country.setContinent("Europe");
			countries.add(country);
		}
		
		//Insercao dos paises da America do Norte
		countryList = nodeList.extractAllNodesThatMatch(
				new LinkStringFilter("http://www.worldatlas.com/webimage/countrys/namerica/"), true);
		iterator = countryList.elements();
		while (iterator.hasMoreNodes()){
			Country country = new Country();
			country.setName(((LinkTag)iterator.nextNode()).getLinkText());
			country.setContinent("North America");
			countries.add(country);
		}
		
		//Insercao dos paises da Oceania
		countryList = nodeList.extractAllNodesThatMatch(
				new LinkStringFilter("http://www.worldatlas.com/webimage/countrys/oceania/"), true);
		iterator = countryList.elements();
		while (iterator.hasMoreNodes()){
			Country country = new Country();
			country.setName(((LinkTag)iterator.nextNode()).getLinkText());
			country.setContinent("Oceania");
			countries.add(country);
		}
		
		//Insercao dos paises da America do Sul
		countryList = nodeList.extractAllNodesThatMatch(
				new LinkStringFilter("http://www.worldatlas.com/webimage/countrys/samerica/"), true);
		iterator = countryList.elements();
		while (iterator.hasMoreNodes()){
			Country country = new Country();
			country.setName(((LinkTag)iterator.nextNode()).getLinkText());
			country.setContinent("South America");
			countries.add(country);
		}
		return countries;

	}
	
	public static void main (String args[]){
		try {
			Vector<Country> countries = parse();
			ListIterator<Country> i = countries.listIterator();
			DBDumper dumper = new DBDumper("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:endemics;create=true", "", "");
			Connection con = dumper.initDB();
			ICountryDao countryDao = new JDBCCountryDao(con);
			while(i.hasNext()){
				countryDao.create(i.next());
			}
			con.close();
			System.out.println("Todos os dados foram adicionados com sucesso!");
		} catch (ParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}
